<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport( 'joomla.application.component.view');
  
  class GameStatViewGameStat extends JView
  {              
    var $_name = "gamestat";
  
    function display()
    { 
      $this->displayList();
    }
    
    function displayList()
    {
      /* sorting */
      
      $state =& $this->get( 'state' );
            $lists['order_Dir'] = $state->get($this->_name . 'filter_order_Dir' );
            $lists['order']     = $state->get($this->_name . 'filter_order' );
            $this->assignRef('lists', $lists);    

      $gamestat = $this->getModel('gamestat');
      $stats    = $gamestat->get_list_summary();
      $this->assign('stats', $stats);
      
      $pagination = $this->get('Pagination');
      $this->assign('pagination', $pagination);
      
      JToolBarHelper::title( JText::_( 'Game Stats' ), 'generic.png' );  
      
      JToolBarHelper::publishList();
      JToolBarHelper::unpublishList();
      JToolBarHelper::addNew();
      JToolBarHelper::editList();
      JToolBarHelper::deleteList();
      
      PBNYHelper::createMenu();
      
      parent::display("list");    
    }
    
    function displayAdd()
    {    
      /* sorting */
      $state =& $this->get( 'state' );
            $lists['order_Dir'] = $state->get($this->_name . 'filter_order_Dir' );
            $lists['order']     = $state->get($this->_name . 'filter_order' );
            $this->assignRef('lists', $lists);    
    
      $team      = $this->getModel('team');
      $teams    = $team->get();
      $this->assign('teams', $teams);
      
      $schedule  = $this->getModel('schedule');
      $schedules = $this->prepare_schedules($schedule->get());
      $this->assign('schedules', $schedules);
      
      $game_end_status = $this->getModel('gameendstatus');
      $game_end_statuses = $game_end_status->get();
      $this->assign('game_end_statuses', $game_end_statuses);
      
      JToolBarHelper::title( JText::_( 'Add Game Stat' ), 'generic.png' );
      JToolBarHelper::save();
      JToolBarHelper::cancel();

      parent::display("edit");
    }    
    
    function displayEdit()
    {    
      
      $cids = JRequest::getVar('cid', null, 'default', 'array' );
      
      $mteam      = $this->getModel('team');
      $mschedule     = $this->getModel('schedule');
      $mgamestat       = $this->getModel('gamestat');
      
      
      $stat       = $mgamestat->get($cids[0]);
      $schedule       = $mschedule->get($stat->schedule_id);
        # add attributes to stat
        $stat->first_team_id  = $schedule->first_team_id;
        $stat->second_team_id = $schedule->second_team_id;
      $team           = $mteam->get($schedule->first_team_id);
      $this->assign('stat', $stat);
      $this->assign('schedules', $this->prepare_schedules(array($schedule)));
      
      $teams = $mteam->get(array($stat->first_team_id, $stat->second_team_id));
      $this->assign('teams', $teams);
      
      $game_end_status = $this->getModel('gameendstatus');
      $game_end_statuses = $game_end_status->get();
      $this->assign('game_end_statuses', $game_end_statuses);

      JToolBarHelper::title( JText::_( 'Edit Game Stat' ), 'generic.png' );
      JToolBarHelper::save();
      JToolBarHelper::cancel();  

      parent::display("edit");
    }
    
    private function prepare_schedules($schedules)
    {
      $team = $this->getModel('team');
      foreach($schedules as $schedule){
        $old_data           = $schedule->schedule;
        $team1              = $team->get($schedule->first_team_id)->name;
        $team2              = $team->get($schedule->second_team_id)->name;
        $schedule->schedule = "$old_data   $team1 vs. $team2";
      }
      return $schedules;
    }
  }
?>